package cn.com.yusys.yusp.mobile.service;

import cn.com.yusys.yusp.admin.Utils.IDUtils;
import cn.com.yusys.yusp.admin.Utils.Smstoone;
import cn.com.yusys.yusp.commons.mapper.CommonMapper;
import cn.com.yusys.yusp.commons.service.CommonService;
import cn.com.yusys.yusp.mobile.repository.mapper.OcrmSMSModelInfoMapper;
import com.esms.MessageData;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 短信模版
 *
 * @author zoie
 */
@Service
public class OcrmSMSModelInfoService extends CommonService {

    private static Logger logger = LoggerFactory.getLogger(OcrmSMSModelInfoService.class);
    @Resource
    private OcrmSMSModelInfoMapper ocrmSMSModelInfoMapper;
    @Resource
    private Smstoone smstoone;

    @Override
    protected CommonMapper getMapper() {
        return null;
    }

    /**
     * 查询短信模版
     *
     * @return
     */
    public List<Map<String, Object>> querySmsModelInfoList() {
        List<Map<String, Object>> list = ocrmSMSModelInfoMapper.querySmsModelInfoList();
        return list;
    }

    /**
     * 短信发送
     * @param queryModel
     */
    public void sendMessage(Map map) {
        long startTime = System.currentTimeMillis();
        System.out.println(Thread.currentThread().getName() + "：短信发送");
        //无返回值
        List<String> arr = new ArrayList<>();
        arr.add("dlscrm1");
        arr.add("dlscrm2");
        String phoneNum = map.get("phoneNum").toString();
        String content = map.get("content").toString();
        map.put("id", IDUtils.getGuid());
        map.put("instanceId", IDUtils.getGuid());
        MessageData messageData = new MessageData(phoneNum, content);
        List<MessageData> sendData = new ArrayList<>();
        sendData.add(messageData);
        try {
            if (sendData != null) {
                logger.info("正在消费数据包" + sendData);
//              System.out.println("正在消费数据包"+senddata);
//              System.out.println("<==================================================================>");
                //发送短信
                smstoone.sendMessage(sendData, arr);
            }
            ocrmSMSModelInfoMapper.insertSendLog(map);
            long endTime = System.currentTimeMillis();
            System.out.println(Thread.currentThread().getName() + "：短信发送结束，耗时：" + (endTime - startTime));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}